package com.example.demo.service.impl;

import com.example.demo.entity.Safetyinspectioncoalmineattributes;
import com.example.demo.entity.VO.WellSpaceVO;
import com.example.demo.handle.CoalMineAttributesListParseHandler;
import com.example.demo.handle.SpatialCoordinatesListParseHandler;
import com.example.demo.mapper.SafetyinspectioncoalmineattributesMapper;
import com.example.demo.service.SafetyinspectioncoalmineattributesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-12
 */
@Service
public class SafetyinspectioncoalmineattributesServiceImpl extends ServiceImpl<SafetyinspectioncoalmineattributesMapper, Safetyinspectioncoalmineattributes> implements SafetyinspectioncoalmineattributesService {

    @Autowired
    private SafetyinspectioncoalmineattributesMapper safetyinspectioncoalmineattributesMapper;

    @Override
    public Integer insertCoalMineAttributesData(String coalMineInformationXml,String wellheadSpaceXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            CoalMineAttributesListParseHandler coalMineAttributesListParseHandler = new CoalMineAttributesListParseHandler();
            SpatialCoordinatesListParseHandler spatialCoordinatesListParseHandler = new SpatialCoordinatesListParseHandler();
            try {
                // 读取煤矿信息文件
                parser.parse(coalMineInformationXml, coalMineAttributesListParseHandler);

                //读取井口信息文件
                parser.parse(wellheadSpaceXml, spatialCoordinatesListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + coalMineAttributesListParseHandler.getSafetyinspectioncoalmineattributesList().size() + "个煤矿信息");
            System.out.println("~~~~~这个煤矿信息共有" + spatialCoordinatesListParseHandler.getSafetyinspectionspatialcoordinatesList().size() + "个井口信息");
            // 根据minecode去查找数据库
            for (Safetyinspectioncoalmineattributes safetyinspectioncoalmineattributes : coalMineAttributesListParseHandler.getSafetyinspectioncoalmineattributesList()) {
                System.out.println(safetyinspectioncoalmineattributes.getCsMineCode());
                System.out.println(safetyinspectioncoalmineattributes.getCsDataTime());
                System.out.println(safetyinspectioncoalmineattributes.getCsMineName());
                System.out.println(safetyinspectioncoalmineattributes.getCsMineShortname());
                System.out.println(safetyinspectioncoalmineattributes.getCsMineAddr());
                System.out.println(safetyinspectioncoalmineattributes.getCsBtareaCode());
                System.out.println(safetyinspectioncoalmineattributes.getCsMinePhone());
                System.out.println(safetyinspectioncoalmineattributes.getCsAdministerCode());
                System.out.println(safetyinspectioncoalmineattributes.getCsAdministerName());
                System.out.println(safetyinspectioncoalmineattributes.getCsSubrelationCode());
                System.out.println(safetyinspectioncoalmineattributes.getCsMinelicenNum());
                System.out.println(safetyinspectioncoalmineattributes.getCsMinelicenEffec());
                System.out.println(safetyinspectioncoalmineattributes.getCsSplicenNum());
                System.out.println(safetyinspectioncoalmineattributes.getCsSplicenEffec());
                System.out.println(safetyinspectioncoalmineattributes.getCsProlicenNum());
                System.out.println(safetyinspectioncoalmineattributes.getCsProlicenEffec());
                System.out.println(safetyinspectioncoalmineattributes.getCsBusilicenNum());
                System.out.println(safetyinspectioncoalmineattributes.getCsBusilicenEffec());
                System.out.println(safetyinspectioncoalmineattributes.getCsMineheadName());
                System.out.println(safetyinspectioncoalmineattributes.getCsMheadlicenNum());
                System.out.println(safetyinspectioncoalmineattributes.getCsMheadlicenEffec());
                System.out.println(safetyinspectioncoalmineattributes.getCsMheadqualiNum());
                System.out.println(safetyinspectioncoalmineattributes.getCsEconomicType());
                System.out.println(safetyinspectioncoalmineattributes.getCsCapacityAnnual());
                System.out.println(safetyinspectioncoalmineattributes.getCsCapacityReal());
                System.out.println(safetyinspectioncoalmineattributes.getCsWorkersTotal());
                System.out.println(safetyinspectioncoalmineattributes.getCsMineField());
                System.out.println(safetyinspectioncoalmineattributes.getCsCoalSeam());
                System.out.println(safetyinspectioncoalmineattributes.getCsGasLevel());
                System.out.println(safetyinspectioncoalmineattributes.getCsGasAbs());
                System.out.println(safetyinspectioncoalmineattributes.getCsGasCom());
                System.out.println(safetyinspectioncoalmineattributes.getCsMineState());
                System.out.println("----------------------------煤矿信息finish---------------------------------");
//                personnelpositioningattendanceinfoMapper.insertMsg(personnelpositioningattendanceinfo.getId(),personnelpositioningattendanceinfo.getCardNum(),personnelpositioningattendanceinfo.getOutTime().toString(),personnelpositioningattendanceinfo.getInTime().toString(),personnelpositioningattendanceinfo.getEmployeeId(),personnelpositioningattendanceinfo.getWorkTeamDay(),personnelpositioningattendanceinfo.getTeamName());

                // 我要根据每一个煤矿信息来查询这个煤矿信息cs_mine_code对应的cs_well_code和cs_well_name
                int i = 1;
                System.out.println("这个煤矿信息的cs_mine_code:" + safetyinspectioncoalmineattributes.getCsMineCode());
                for (WellSpaceVO wellSpaceVO : spatialCoordinatesListParseHandler.getSpaceVOArrayList()) {
                    if (safetyinspectioncoalmineattributes.getCsMineCode().equals(wellSpaceVO.getCsMineCode())) {
                        System.out.println(wellSpaceVO.getCsMineCode());
                        System.out.println(wellSpaceVO.getCsWellCode());
                        System.out.println(wellSpaceVO.getCsWellName());
                        System.out.println(wellSpaceVO.getCsYCoor());
                        System.out.println(wellSpaceVO.getCsXCoor());
                        System.out.println(wellSpaceVO.getCsXCoor());
                        System.out.println(safetyinspectioncoalmineattributes.getCsMineCode() + "------------------------------------的第" + i + "井口信息finish-------------------------------------------");

                        // 插入
                        safetyinspectioncoalmineattributesMapper.insertCoalMineAttributesData(safetyinspectioncoalmineattributes.getCsMineCode(),safetyinspectioncoalmineattributes.getCsDataTime(),safetyinspectioncoalmineattributes.getCsMineName(),safetyinspectioncoalmineattributes.getCsMineShortname(),wellSpaceVO.getCsWellCode(),wellSpaceVO.getCsWellName(),safetyinspectioncoalmineattributes.getCsMineAddr(),safetyinspectioncoalmineattributes.getCsBtareaCode(),safetyinspectioncoalmineattributes.getCsMinePhone(),safetyinspectioncoalmineattributes.getCsAdministerCode(),safetyinspectioncoalmineattributes.getCsAdministerName(),safetyinspectioncoalmineattributes.getCsSubrelationCode(),safetyinspectioncoalmineattributes.getCsMheadlicenNum(),safetyinspectioncoalmineattributes.getCsMinelicenEffec(),safetyinspectioncoalmineattributes.getCsSplicenNum(),safetyinspectioncoalmineattributes.getCsSplicenEffec(),safetyinspectioncoalmineattributes.getCsProlicenNum(),safetyinspectioncoalmineattributes.getCsProlicenEffec(),safetyinspectioncoalmineattributes.getCsBusilicenNum(),safetyinspectioncoalmineattributes.getCsBusilicenEffec(),safetyinspectioncoalmineattributes.getCsMineheadName(),safetyinspectioncoalmineattributes.getCsMinelicenNum(),safetyinspectioncoalmineattributes.getCsMheadlicenEffec(),safetyinspectioncoalmineattributes.getCsMheadqualiNum(),safetyinspectioncoalmineattributes.getCsEconomicType(),safetyinspectioncoalmineattributes.getCsCapacityAnnual(),safetyinspectioncoalmineattributes.getCsCapacityReal(),safetyinspectioncoalmineattributes.getCsWorkersTotal(),safetyinspectioncoalmineattributes.getCsMineField(),safetyinspectioncoalmineattributes.getCsCoalSeam(),safetyinspectioncoalmineattributes.getCsGasLevel(),safetyinspectioncoalmineattributes.getCsGasAbs(),safetyinspectioncoalmineattributes.getCsGasCom(),safetyinspectioncoalmineattributes.getCsMineState());
                        i++;
                    }

                }
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
